
package com.android.matmanual;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;

public class FMAutoTest extends Activity {
	TextView tv;
	HeadsetPlugReceiver receiver;
	IntentFilter mIntentFilter;
	Boolean bool = false;
	Boolean fmstart = false;
	AudioManager mAudioManager;
	WakeLock mWakeLock;	
	public void stopFM()
	{
		Intent tent = new Intent("com.thunderst.radio.RadioService.SHUTDOWN");
		sendBroadcast(tent);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fmautotest);
		setTitle(R.string.fmtest_name);
	    PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 
		tv = (TextView)findViewById(R.id.tv);
		
		mIntentFilter = new IntentFilter();
		receiver = new HeadsetPlugReceiver();
		mIntentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
		registerReceiver(receiver, mIntentFilter); 
		tv.setText(R.string.fmclose);
		
		mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		bool = mAudioManager.isWiredHeadsetOn();
		if(bool)
		{
		    Intent tent = new Intent("com.thunderst.radio.RADIO");
		    tent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        FMAutoTest.this.startActivity(tent);
			fmstart = true;
		}
		else
		{
		    tv.setText(R.string.insertheadset);
		}
	}
	
	@Override
	protected void onResume() {
	    // TODO Auto-generated method stub
	    super.onResume();
	    if(fmstart)
	    {
	        stopFM();
	        fmstart = false;
	    }
	}
	
	public class HeadsetPlugReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			 if(arg1.getIntExtra("state", 0) == 1){
				bool = true;
				Intent tent = new Intent("com.thunderst.radio.RADIO");
				tent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        FMAutoTest.this.startActivity(tent);
				fmstart = true;
			 }
			 else
			 {
				 bool = false;
				 tv.setText(R.string.insertheadset);
			 }
		}
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if(fmstart){
			stopFM();
		}
		super.onStop();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
		
		unregisterReceiver(receiver);
		super.onDestroy();
	}
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
    }
    
    @Override
    public void onBackPressed() {
        
    }
    
   @Override
   public void onAttachedToWindow() 
   {
       // TODO Auto-generated method stub
       super.onAttachedToWindow();
       this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
   }
   
}
		 